<?php
	class ArrayToCSV{
		
		var $path;
		var $headers;
		var $columnNames;
		var $values;
		var $maxValues;
		
		function ArrayToCSV($path,$headers,$values){
			$this->path = $path;
			$this->headers = $headers;
			$this->values = $values;
			$this->columnNames = array();
			$this->maxValues;
		}
		
		function maxCount($array){
			$max = 0;
			while(is_array($array)){
				$max = count($array);
				$array = reset($array);
			}
			return $max;
		}
		
		function makeHeaders($index,$array){
			$columns = array_keys($array);
			foreach($columns as $column){
				$nbFields = (is_array($array))? count(reset($array)) - 1 : 1;
				//for($i=0;$i<floor($nbFields / 2);$i++){$this->columnNames[$index][] = "";}
				$this->columnNames[$index][] = (is_string($column)) ? iconv("UTF-8", "Windows-1252//TRANSLIT", $column) : $column;
				$columnIndex = count($this->columnNames[$index]);
				for($i=0;$i<$this->maxCount($array[$column]) - 1;$i++){$this->columnNames[$index][] = "";}
				if(is_array($array[$column])) $this->makeHeaders($index + 1,$array[$column]);
			}
		}
		
		function setHeaders($file){
			foreach($this->columnNames as $nameLines){
				fputcsv($file, $nameLines, ';');
			}
		}
		
		function setValues($file){
			foreach($this->values as $data) {
					$selectionLine = array();
					$timeLine = array();
					$selectionLine[] = $data['sessionStatus'];
					$timeLine[] = "";
					$selectionLine[] = $data['sessionInfos']['sessionId'];
					$timeLine[] = "";
					$selectionLine[] = $data['sessionInfos']['date'];
					$timeLine[] = "";
					$selectionLine[] = $data['sessionInfos']['induction'];
					$timeLine[] = "";
	
					$selectionLine[] = $data['participantInfos']['participantId'];
					$timeLine[] = "";
					$selectionLine[] = $data['participantInfos']['birthMonth'].'/'.$data['participantInfos']['birthYear'];
					$timeLine[] = "";
					$selectionLine[] = ($data['participantInfos']['gender'] == 1)? 'M':'F';
					$timeLine[] = "";
					
					if(!empty($data['panasInfos'])){
						foreach($data['panasInfos'] as $key => $value){
							$selectionLine[] = $value;
							$timeLine[] = "";
						}
					}
					else
					{
						for($i = 0;$i<20;$i++){
							$selectionLine[] = "";
							$timeLine[] = "";
						}
					}
					
					//if($data['answerData'] != NULL){
						foreach($data['answerData'] as $answerData){
							foreach(array_keys($this->headers["Réponses"]) as $pairColor){
								foreach(array_keys($this->headers["Réponses"][$pairColor]) as $shape){
									if(isset($answerData[$pairColor][$shape]['chosenColorName'])){
										$selectionLine[] = $answerData[$pairColor][$shape]['chosenColorName'];
										$timeLine[] = $answerData[$pairColor][$shape]['time'];
									}
									else
									{
										$selectionLine[] = "";
										$timeLine[] = "";
									}
								}
							}
						}	
					//}
					fputcsv($file, $selectionLine, ';');
					fputcsv($file, $timeLine, ';');	
				}
		}
		
		function exportToCSV(){
			$file = @fopen($this->path, 'w');
			if($file === false) throw new Exception("Le fichier n'a pas pu être créé.");
			$this->makeHeaders(0,$this->headers);
			$this->setHeaders($file);
			$this->setValues($file);
			header('Content-disposition: attachment; filename='.$this->path);
			header('Content-Type: application/force-download');
			header('Content-Length: '.filesize($this->path));
			header('Pragma: no-cache');
			header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
			header('Expires: 0');
			readfile($this->path);
			fclose($file);
		}
	}
?>